Zhiyong Zhang's Psychometric Website
 
Home Control Panel Login Register Search Submit Logout About me Contact me
My Research
Google
Web
This Site
Home > SAS WinBUGS for linear regression
SAS WinBUGS for linear regression
2008-05-16          Read: 11427 times
Cite this page: (2008). SAS WinBUGS for linear regression. Retrieved December 22, 2024, from https://www.psychstat.org/us/article.php/73.htm.
SAS WinBUGS for linear regression

Please cite this using

Zhang, Z., McArdle, J. J., Wang, L., & Hamagami, F. (2008). A SAS interface for Bayesian analysis with WinBUGS. Structural Equation Modeling, 15(4), 705?C728.

TITLE 'Run WinBUGS from SAS: A Multiple Regression Example';
TITLE2 'Generate the Data';

DATA Sim_Reg;
  b0=1; b1=2; b2=3; sig_e=2; seed=20060118; N = 1000;
  DO _N_ = 1 TO N;
        x1=RANNOR(seed);
  x2=RANNOR(seed);
  e=RANNOR(seed);
     y = b0+b1*x1+b2*x2+sig_e*e;
   KEEP y x1 x2;
   OUTPUT;
   END;
RUN;

PROC REG data=Sim_Reg;
MODEL y=x1 x2;
RUN;

FILENAME model "c:\RegModel.txt";
DATA model;
INPUT model \$80.;
CARDS;/*start the model*/
model{
#Model specification
   for (i in 1:N) {
      y[i]~dnorm(muy[i], Inv_sig2_e)
   muy[i]<-b0+b1*x1[i]+b2*x2[i]
   }
#priors
   b0~dnorm(0, 1.0E-6)
   b1~dnorm(0, 1.0E-6)
   b2~dnorm(0, 1.0E-6)
   Inv_sig2_e~dgamma(1.0E-3, 1.0E-3)
#parameter transformation
   Sig2_e<-1/Inv_sig2_e
}
;
RUN;
DATA _NULL_;
  SET model;
  FILE model;
  PUT model;
RUN;

%_lexport(data=Sim_Reg, file='c:\RegData.txt',
var=y x1 x2);

/*Create the initial values*/
DATA _NULL_;
FILE "c:\RegInit.txt";
PUT "list(b0=0, b1=0, b2=0, Inv_sig2_e=1)";
RUN;

/*Create a script file that includes the command to be used in WinBUGS*/
DATA _NULL_;
FILE "C:\Programs\WinBUGS14\BatchReg.txt";
PUT // @@
#1 "display('log')"
#2 "check('c:/RegModel.txt')"
#3 "data('c:/RegData.txt')"
#4 "compile(1)"
#5 "inits(1, 'c:/RegInit.txt')"
#6 "gen.inits()"
#7 "update(2000)"
#8 "set(b0)"
#9 "set(b1)"
#10 "set(b2)"
#11 "set(Sig2_e)"
#12 "dic.set()"
#13 "update(5000)"
#14 "dic.stats()"
#15 "coda(*,'c:/output')"
#16 "save('c:/bugslog.txt')"
#17 "quit()"
;
RUN;

DATA _NULL_;
FILE "c:\run.bat";
PUT 'CD C:\programs\WinBUGS14';
PUT 'WinBUGS14.exe /PAR BatchReg.txt';
PUT 'exit';
RUN;

DATA _NULL_;
X "c:\run.bat";
RUN;
QUIT;

/*Read in the log file to view the DIC*/
DATA log;
INFILE "c:\bugslog.txt" TRUNCOVER ;
INPUT log \$80.;
log=translate(log," ","09"x);
RUN;

PROC PRINT DATA=log;
RUN;

%coda2sas(out=post1, infile='c:\outputIndex.txt',
chain='c:\output1.txt', stats=1);
QUIT;

Submitted by: johnny
Add a comment View comment Add to my favorite Email to a friend Print
If you want to rate, please login first, or click here to register. Or you can use USERNAME: guest and PASSWORD: guest to log in.
Average score 0, based on 0 comments
1 2 3 4 5 6 7 8 9 10
Copyright © 2003-13 Zhiyong Zhang \'s Psychometric Website
Maintained by Zhiyong Zhang